package com.young.ysepay.service.impl;

import com.alibaba.fastjson.JSON;
import com.young.ysepay.controllers.YsPaymentControllrt;
import com.young.ysepay.dto.YsPayDTO;
import com.young.ysepay.service.YsBuildRequestParaService;
import com.young.ysepay.service.YsBusinessPramService;
import com.young.ysepay.service.YsPaymentService;
import com.young.ysepay.service.YsVerifyJsonSignService;
import com.young.ysepay.utils.DateFormatHelper;
import com.young.ysepay.utils.Https;
import com.young.ysepay.utils.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: WANGNING
 * @Email wyn20140226@aliyun.com
 * @Date: 2019/6/4 18:45
 * @Des
 */
@Service
public class YsPaymentServiceImpl implements YsPaymentService {
    @Autowired
    private YsBusinessPramService ysBusinessPramService;
    @Autowired
    private YsBuildRequestParaService ysBuildRequestParaService;
    @Autowired
    private YsVerifyJsonSignService ysVerifyJsonSignService;
    private static Logger log = LoggerFactory.getLogger(YsPaymentControllrt.class);
    @Override
    public Map<String, Object> payment(Map<String, Object> mapParams) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Object temp = mapParams.get("biz_content");
        String bizContent = temp !=null ? temp.toString(): null;
        YsPayDTO ysPayDTO=this.ysBusinessPramService.ysBusinessPram();
        //封装公共参数以及业务参数
        Map<String, String> paramsMap = new HashMap<>();
        //加急
        paramsMap.put("method", "ysepay.df.single.quick.accept");
        paramsMap.put("partner_id", ysPayDTO.getPARTNER_ID());
        paramsMap.put("timestamp", DateFormatHelper.getNowTimeStr());
        paramsMap.put("charset", ysPayDTO.getDEFAULT_CHARSET());
        paramsMap.put("sign_type", ysPayDTO.getSIGN_ALGORITHM());
        paramsMap.put("version", ysPayDTO.getVERSION());
        paramsMap.put("notify_url", ysPayDTO.getNOTIFY_TRANSFER_URL());
        paramsMap.put("biz_content", bizContent);
        try {
            Map<String, String> sParaMap = ysBuildRequestParaService.buildRequestPara(paramsMap);
            log.error("银盛非一清待打款接口参数："+paramsMap);
            String returns = Https.httpsSend(ysPayDTO.getYSEPAY_GATEWAY_URL_DSF(), sParaMap);
            log.info("银盛非一清待打款接口返回信息:"+returns);
            resultMap = (Map) JSON.parse(returns);
            Object obj = resultMap.get("ysepay_df_single_quick_accept_response");
            boolean verifyResult = false;
            if(obj != null){
                Map<String, Object> ysepay_df_single_quick_accept_response = (Map) JSON.parseObject(obj.toString());
                int yscount = returns.toString().indexOf("ysepay_df_single_quick_accept_response");
                String str = returns.substring(yscount);
                str = str.replace("ysepay_df_single_quick_accept_response", "");
                str = str.substring(2, str.length() - 1);
                verifyResult = ysVerifyJsonSignService.verifyJsonSign(resultMap.get("sign").toString(), str, "utf-8");
               /* ysepay_df_single_quick_accept_response.put("sign", resultMap.get("sign"));
                resultMap.putAll(ysepay_df_single_quick_accept_response);*/
                log.error("YsPaymentServiceImpl -------------------payment"+"校验是否成功"+verifyResult+resultMap);

            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("银盛非一清待打款接口"+e.toString());
        } finally {
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> paymentQuery(Map<String, Object> mapParams) {
        log.info("YsPaymentServiceImpl________________paymentQuery"+JSON.toJSONString(mapParams));
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Object temp = mapParams.get("biz_content");
        String bizContent = temp !=null ? temp.toString(): null;
        YsPayDTO ysPayDTO=this.ysBusinessPramService.ysBusinessPram();
        //封装公共参数以及业务参数
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("method", "ysepay.df.single.quick.accept");
        paramsMap.put("partner_id", ysPayDTO.getPARTNER_ID());
        paramsMap.put("timestamp", DateFormatHelper.getNowTimeStr());
        paramsMap.put("charset", ysPayDTO.getDEFAULT_CHARSET());
        paramsMap.put("sign_type", ysPayDTO.getSIGN_ALGORITHM());
        paramsMap.put("version", ysPayDTO.getVERSION());
        paramsMap.put("biz_content", bizContent);
        try {
            Map<String, String> sParaMap = ysBuildRequestParaService.buildRequestPara(paramsMap);
            log.info("银盛非一清待打款查询接口参数："+sParaMap.toString());
            String returns = Https.httpsSend(ysPayDTO.getYSEPAY_GATEWAY_URL_DSF(), sParaMap);
            log.info("银盛非一清待打款接口返回信息:"+returns);
            resultMap = (Map) JSON.parse(returns);
            Object obj = resultMap.get("ysepay_df_single_quick_accept_response");
            boolean verifyResult = false;
            if(obj != null){
                Map<String, Object> ysepay_df_single_quick_accept_response = (Map) JSON.parseObject(obj.toString());
                int yscount = returns.toString().indexOf("ysepay_df_single_quick_accept_response");
                String str = returns.substring(yscount);
                str = str.replace("ysepay_df_single_quick_accept_response", "");
                str = str.substring(2, str.length() - 1);
                verifyResult = ysVerifyJsonSignService.verifyJsonSign(resultMap.get("sign").toString(), str, "utf-8");
                ysepay_df_single_quick_accept_response.put("sign", resultMap.get("sign"));
                resultMap.putAll(ysepay_df_single_quick_accept_response);
                log.info("YsPaymentServiceImpl -------------------paymentQuery"+"校验是否成功"+verifyResult+resultMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("银盛非一清待打款查询接口"+e.getMessage());
        } finally {
            return resultMap;
        }
    }
}
